import { Router, createRouter, createWebHistory } from 'vue-router'
import { getCookie } from '@/utils';

const router : Router = createRouter({
	history: createWebHistory(),
	routes: [
		{
			path: '/',
			redirect: '/businesspush'
		},
		{
			path: '/404',
			name: '404',
			component: () => import('@/pages/404.vue')
		},
		{
			name: 'Login',
			path: '/login',
			meta: {
				title: '登录'
			},
			component: () => import('@/pages/login/index.vue')
		},
		{
			name: 'BankList',
			path: '/banklist',
			meta: {
				title: '我的银行卡'
			},
			component: () => import('@/pages/banklist/index.vue')
		},
		{
			name: 'BankAdd',
			path: '/bankadd',
			meta: {
				title: '添加银行卡'
			},
			component: () => import('@/pages/bankadd/index.vue')
		},
		{

			path: "/:pathMath(.*)", // 此处需特别注意置于最底部
			redirect: "/404"
		},
		{
			name: 'Home',
			path: '/home',
			meta: {
				title: '首页'
			},
			component: () => import('@/pages/home/index.vue')
		},
		{
			name: 'User',
			path: '/user',
			meta: {
				title: '个人中心'
			},
			component: () => import('@/pages/user/index.vue')
		},
		{
			name: 'BusinessPush',
			path: '/businesspush',
			meta: {
				title: '发布转单'
			},
			component: () => import('@/pages/push/index.vue')
		},
		{
			name: 'BusinessEdit',
			path: '/busineseedit/:id',
			meta: {
				title: '编辑商机'
			},
			component: () => import('@/pages/push/index.vue')
		},
		{
			name: 'Order',
			path: '/orderlist',
			meta: {
				title: '转单记录'
			},
			component: () => import('@/pages/orderlist/index.vue')
		},
		{
			name: 'Sale',
			path: '/salelist',
			meta: {
				title: '销售记录'
			},
			component: () => import('@/pages/salelist/index.vue')
		},
		{
			name: 'AfterSale',
			path: '/aftersalelist',
			meta: {
				title: '售后记录'
			},
			component: () => import('@/pages/aftersalelist/index.vue')
		},
		{
			name: 'Detail',
			path: '/detail/:id',
			meta: {
				title: '采购单详情'
			},
			component: () => import('@/pages/detail/index.vue')
		},
		{
			name: 'WidthDraw',
			path: '/widthdraw',
			meta: {
				title: '提现',
				keep: true
			},
			component: () => import('@/pages/widthdraw/index.vue')
		},
		{
			name: 'Record',
			path: '/record',
			meta: {
				title: '明细'
			},
			component: () => import('@/pages/record/index.vue')
		},
		{
			name: 'BuyRecord',
			path: '/buyrecord',
			meta: {
				title: '购买记录'
			},
			component: () => import('@/pages/buyrecord/index.vue')
		},
		{
			name: 'AfterBuy',
			path: '/afterBuy',
			meta: {
				title: '售后管理'
			},
			component: () => import('@/pages/afterBuy/index.vue')
		},
		{
			name: 'AddFankui',
			path: '/addFankui',
			meta: {
				title: '新增反馈'
			},
			component: () => import('@/pages/addFankui/index.vue')
		},
		{
			name: 'FankuiDetail',
			path: '/fankuiDetail',
			meta: {
				title: '反馈详情'
			},
			component: () => import('@/pages/fankuiDetail/index.vue')
		},
		{
			name: 'AddNewxx',
			path: '/addNewxx',
			meta: {
				title: '提交线索'
			},
			component: () => import('@/pages/addNewxx/index.vue')
		},
		{
			name: 'EditFankui',
			path: '/editFankui',
			meta: {
				title: '反馈修改'
			},
			component: () => import('@/pages/editFankui/index.vue')
		},
		{
			name: 'myPro',
			path: '/myPro',
			meta: {
				title: '我的推广'
			},
			component: () => import('@/pages/myPro/index.vue')
		},
		{
			name: 'incomeRecord',
			path: '/incomeRecord',
			meta: {
				title: '收益记录'
			},
			component: () => import('@/pages/incomeRecord/index.vue')
		},
		{
			name: 'openTicket',
			path: '/openTicket',
			meta: {
				title: '开票管理'
			},
			component: () => import('@/pages/openTicket/index.vue')
		},
		{
			name: 'takeTicket',
			path: '/takeTicket',
			meta: {
				title: '申请开票'
			},
			component: () => import('@/pages/takeTicket/index.vue')
		},
	]
})
router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title as any
		console.log(from)
	}
	/* 路由守卫逻辑 */
	if (!getCookie('token') && to.path !== '/login') {
		next({ replace: true, path: `/login`, query: { redirect: to.path } })
	} else {
		next()
	}


})
export default router